import { ref, computed, watch } from 'vue'

/**
 * 分页组合式函数
 * @param options 分页配置选项
 */
export function usePagination(options: {
  pageSize?: number
  total?: number
  currentPage?: number
  showSizeChanger?: boolean
  showQuickJumper?: boolean
  pageSizeOptions?: number[]
} = {}) {
  const {
    pageSize = 10,
    total = 0,
    currentPage = 1,
    showSizeChanger = false,
    showQuickJumper = false,
    pageSizeOptions = [10, 20, 50, 100]
  } = options

  const current = ref(currentPage)
  const size = ref(pageSize)
  const totalCount = ref(total)

  const totalPages = computed(() => Math.ceil(totalCount.value / size.value))
  
  const hasPrev = computed(() => current.value > 1)
  const hasNext = computed(() => current.value < totalPages.value)

  const pageList = computed(() => {
    const pages = []
    const maxPages = 7 // 最多显示的页码数
    const half = Math.floor(maxPages / 2)
    
    let start = Math.max(1, current.value - half)
    let end = Math.min(totalPages.value, start + maxPages - 1)
    
    if (end - start + 1 < maxPages) {
      start = Math.max(1, end - maxPages + 1)
    }
    
    for (let i = start; i <= end; i++) {
      pages.push({
        page: i,
        active: i === current.value,
        disabled: false
      })
    }
    
    return pages
  })

  const changePage = (page: number) => {
    if (page >= 1 && page <= totalPages.value && page !== current.value) {
      current.value = page
      return true
    }
    return false
  }

  const changePageSize = (newSize: number) => {
    if (pageSizeOptions.includes(newSize) && newSize !== size.value) {
      size.value = newSize
      current.value = 1 // 重置到第一页
      return true
    }
    return false
  }

  const prev = () => changePage(current.value - 1)
  const next = () => changePage(current.value + 1)
  const first = () => changePage(1)
  const last = () => changePage(totalPages.value)

  const reset = (newPage = 1, newSize = pageSize) => {
    current.value = newPage
    size.value = newSize
  }

  return {
    // 状态
    current,
    size,
    total: totalCount,
    
    // 计算属性
    totalPages,
    hasPrev,
    hasNext,
    pageList,
    
    // 方法
    changePage,
    changePageSize,
    prev,
    next,
    first,
    last,
    reset,
    
    // 配置
    showSizeChanger,
    showQuickJumper,
    pageSizeOptions
  }
}

/**
 * 表格分页组合式函数
 * @param fetchData 获取数据的函数
 */
export function useTablePagination<T>(
  fetchData: (params: { page: number; pageSize: number }) => Promise<{
    data: T[]
    total: number
  }>
) {
  const loading = ref(false)
  const data = ref<T[]>([])
  
  const pagination = usePagination()
  
  const loadData = async () => {
    if (loading.value) return
    
    loading.value = true
    try {
      const result = await fetchData({
        page: pagination.current.value,
        pageSize: pagination.size.value
      })
      
      data.value = result.data
      pagination.total.value = result.total
    } catch (error) {
      console.error('加载数据失败:', error)
      // 可以在这里添加错误提示
    } finally {
      loading.value = false
    }
  }
  
  // 监听分页变化
  watch([pagination.current, pagination.size], () => {
    loadData()
  })
  
  return {
    loading,
    data,
    pagination,
    loadData
  }
}